﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类ContractLackItemsInfo。
    /// </summary>
    public class ContractLackItemsInfo
    {
        public ContractLackItemsInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid ContractId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Contract_LackItemsInfo where ContractId=@ContractId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, ContractId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractLackItemsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Contract_LackItemsInfo(");
            strSql.Append("ContractId,IsLackItem1,LackItems1,LackOthers1,CompleteDate1,IsLackItem2,LackItems2,LackOthers2,CompleteDate2,Addtime,Updatetime,UpdatePersonId,UpdatePerson)");

            strSql.Append(" values (");
            strSql.Append("@ContractId,@IsLackItem1,@LackItems1,@LackOthers1,@CompleteDate1,@IsLackItem2,@LackItems2,@LackOthers2,@CompleteDate2,@Addtime,@Updatetime,@UpdatePersonId,@UpdatePerson)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "IsLackItem1", DbType.Boolean, model.IsLackItem1);
            db.AddInParameter(dbCommand, "LackItems1", DbType.AnsiString, model.LackItems1);
            db.AddInParameter(dbCommand, "LackOthers1", DbType.AnsiString, model.LackOthers1);
            db.AddInParameter(dbCommand, "CompleteDate1", DbType.DateTime, model.CompleteDate1);
            db.AddInParameter(dbCommand, "IsLackItem2", DbType.Boolean, model.IsLackItem2);
            db.AddInParameter(dbCommand, "LackItems2", DbType.AnsiString, model.LackItems2);
            db.AddInParameter(dbCommand, "LackOthers2", DbType.AnsiString, model.LackOthers2);
            db.AddInParameter(dbCommand, "CompleteDate2", DbType.DateTime, model.CompleteDate2);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "Updatetime", DbType.DateTime, model.Updatetime);
            db.AddInParameter(dbCommand, "UpdatePersonId", DbType.Guid, model.UpdatePersonId);
            db.AddInParameter(dbCommand, "UpdatePerson", DbType.AnsiString, model.UpdatePerson);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ContractLackItemsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Contract_LackItemsInfo set ");
            strSql.Append("IsLackItem1=@IsLackItem1,");
            strSql.Append("LackItems1=@LackItems1,");
            strSql.Append("LackOthers1=@LackOthers1,");
            strSql.Append("CompleteDate1=@CompleteDate1,");
            strSql.Append("IsLackItem2=@IsLackItem2,");
            strSql.Append("LackItems2=@LackItems2,");
            strSql.Append("LackOthers2=@LackOthers2,");
            strSql.Append("CompleteDate2=@CompleteDate2,");
            strSql.Append("Addtime=@Addtime,");
            strSql.Append("Updatetime=@Updatetime,");
            strSql.Append("UpdatePersonId=@UpdatePersonId,");
            strSql.Append("UpdatePerson=@UpdatePerson");
            strSql.Append(" where ContractId=@ContractId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "IsLackItem1", DbType.Boolean, model.IsLackItem1);
            db.AddInParameter(dbCommand, "LackItems1", DbType.AnsiString, model.LackItems1);
            db.AddInParameter(dbCommand, "LackOthers1", DbType.AnsiString, model.LackOthers1);
            db.AddInParameter(dbCommand, "CompleteDate1", DbType.DateTime, model.CompleteDate1);
            db.AddInParameter(dbCommand, "IsLackItem2", DbType.Boolean, model.IsLackItem2);
            db.AddInParameter(dbCommand, "LackItems2", DbType.AnsiString, model.LackItems2);
            db.AddInParameter(dbCommand, "LackOthers2", DbType.AnsiString, model.LackOthers2);
            db.AddInParameter(dbCommand, "CompleteDate2", DbType.DateTime, model.CompleteDate2);
            db.AddInParameter(dbCommand, "Addtime", DbType.DateTime, model.Addtime);
            db.AddInParameter(dbCommand, "Updatetime", DbType.DateTime, model.Updatetime);
            db.AddInParameter(dbCommand, "UpdatePersonId", DbType.Guid, model.UpdatePersonId);
            db.AddInParameter(dbCommand, "UpdatePerson", DbType.AnsiString, model.UpdatePerson);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid ContractId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_LackItemsInfo ");
            strSql.Append(" where ContractId=@ContractId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, ContractId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractLackItemsInfo GetModel(Guid ContractId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ContractId,IsLackItem1,LackItems1,LackOthers1,CompleteDate1,IsLackItem2,LackItems2,LackOthers2,CompleteDate2,Addtime,Updatetime,UpdatePersonId,UpdatePerson from t_Contract_LackItemsInfo ");
            strSql.Append(" where ContractId=@ContractId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, ContractId);
            Chenxin.Lib.Business.Entity.ContractLackItemsInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ContractId,IsLackItem1,LackItems1,LackOthers1,CompleteDate1,IsLackItem2,LackItems2,LackOthers2,CompleteDate2,Addtime,Updatetime,UpdatePersonId,UpdatePerson ");
            strSql.Append(" FROM t_Contract_LackItemsInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得实体及意向书编号
        /// </summary>
        public static DataSet GetModelAndIntentionCode(Guid ContractId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select ContractId,IsLackItem1,LackItems1,LackOthers1,CompleteDate1,IsLackItem2,LackItems2,LackOthers2,CompleteDate2,(Select IntentionCode From t_Intention_BaseInfo Where ContractId = '{0}') As IntentionCode ", ContractId);
            strSql.Append(" FROM t_Contract_LackItemsInfo ");
            strSql.AppendFormat(" where ContractId='{0}' ",ContractId);

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractLackItemsInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ContractId,IsLackItem1,LackItems1,LackOthers1,CompleteDate1,IsLackItem2,LackItems2,LackOthers2,CompleteDate2,Addtime,Updatetime,UpdatePersonId,UpdatePerson ");
            strSql.Append(" FROM t_Contract_LackItemsInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.ContractLackItemsInfo> list = new List<Chenxin.Lib.Business.Entity.ContractLackItemsInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractLackItemsInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ContractLackItemsInfo model = new Chenxin.Lib.Business.Entity.ContractLackItemsInfo();
            object ojb;
            ojb = dataReader["ContractId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ContractId = new Guid(ojb.ToString());
            }
            ojb = dataReader["IsLackItem1"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsLackItem1 = (bool)ojb;
            }
            model.LackItems1 = dataReader["LackItems1"].ToString();
            model.LackOthers1 = dataReader["LackOthers1"].ToString();
            ojb = dataReader["CompleteDate1"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompleteDate1 = (DateTime)ojb;
            }
            ojb = dataReader["IsLackItem2"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsLackItem2 = (bool)ojb;
            }
            model.LackItems2 = dataReader["LackItems2"].ToString();
            model.LackOthers2 = dataReader["LackOthers2"].ToString();
            ojb = dataReader["CompleteDate2"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompleteDate2 = (DateTime)ojb;
            }
            ojb = dataReader["Addtime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Addtime = (DateTime)ojb;
            }
            ojb = dataReader["Updatetime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Updatetime = (DateTime)ojb;
            }
            ojb = dataReader["UpdatePersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdatePersonId = new Guid(ojb.ToString());
            }
            model.UpdatePerson = dataReader["UpdatePerson"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

